package logic

import (
	"core/card"
	"core/component/logger"
)

type RoomPlayer struct {
	owner   *Player
	cards   map[string]int
	isRobot bool
}

func newRoomPlayer(p *TempPlayer, cards []string) *RoomPlayer {
	rp := &RoomPlayer{
		owner:   p.p,
		cards:   make(map[string]int),
		isRobot: p.isRobot,
	}
	rp.addCards(cards)
	return rp
}

func (rp *RoomPlayer) checkCardInHand(cards []string) bool {
	for _, v := range cards {
		_, ok := rp.cards[v]
		if !ok {
			return false
		}
	}
	return true
}

func (rp *RoomPlayer) delCards(cards []string) {
	for _, v := range cards {
		delete(rp.cards, v)
	}
}

func (rp *RoomPlayer) addCards(cards []string) {
	for _, _card := range cards {
		rp.cards[_card] = card.CardsMgrGetMe().CardTurnOff([]string{_card})[0]
	}
}

func (rp *RoomPlayer) robot() bool {
	return rp.isRobot
}

func (rp *RoomPlayer) setRobot() {
	rp.isRobot = true
	logger.Infof("玩家 %v 离开房间，由机器人接管", rp.owner.playerId)
}

func (rp *RoomPlayer) intArrayTurnOffStrArray(above []int) []string {
	cards := make([]string, 0)
	for _, v := range above {
		for cardStr, cardInt := range rp.cards {
			if v == cardInt {
				cards = append(cards, cardStr)
				break
			}
		}
	}
	return cards
}

func (rp *RoomPlayer) cardsToArray() []string {
	cards := make([]string, 0)
	for c, _ := range rp.cards {
		cards = append(cards, c)
	}
	return cards
}
